﻿using Npgsql;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.Model;

namespace WindowsFormsApplication2
{
    public partial class Form3 : Form
    {
        ArrayList layanan;
        ArrayList layananCabang = new ArrayList();
        ArrayList produk;
        DataGridView dgvTransaksi;
        int Id;
        int idTransaksi;
        int idTransaksiLayanan;
        string Name;

        public Form3(DataGridView dgvHome)
        {
            InitializeComponent();

            object[] o = new object[3];
            dgvTransaksi = dgvHome;
            layananCabang = LayananCabang.daftarLayananCabang(Form1.idCabang);
            for (int i = 0; i < layananCabang.Count; ++i)
            {
                o = (object[])layananCabang[i];
                cmbLayanan.Items.Add(o[1]);
            }

            dgvPelayan.DataSource = Karyawan.DaftarKaryawan(Form1.idCabang);
        }

        public Form3(ref ArrayList a, ref ArrayList b, int _idTransaksi, int _Id, string _Name, DataGridView dgvHome)
        {
            InitializeComponent();

            object[] o = new object[4];
            layanan = a;
            produk = b;
            Id = _Id;
            idTransaksi = _idTransaksi;
            idTransaksiLayanan = 0;
            Name = _Name;
            dgvTransaksi = dgvHome;

            layananCabang = LayananCabang.daftarLayananCabang(Form1.idCabang);
            for (int i = 0; i < layananCabang.Count; ++i)
            {
                o = (object[])layananCabang[i];
                cmbLayanan.Items.Add(o[2]);
            }

            dgvPelayan.DataSource = Karyawan.DaftarKaryawan(Form1.idCabang);
        }

        public Form3(ref ArrayList a, ref ArrayList b, int _idTransaksi, int _idTransaksiLayanan, int _Id, string _Name, DataGridView dgvHome)
        {
            InitializeComponent();

            layanan = a;
            produk = b;
            Id = _Id;
            idTransaksi = _idTransaksi;
            idTransaksiLayanan = _idTransaksiLayanan;
            Name = _Name;
            dgvTransaksi = dgvHome;

            object[] o = (object[])layanan[findTransaksiLayanan(_idTransaksiLayanan)];
            object lc = o[5];
            object k = o[4];

            layananCabang = LayananCabang.daftarLayananCabang(Form1.idCabang);
            for (int i = 0; i < layananCabang.Count; ++i)
            {
                o = (object[])layananCabang[i];
                cmbLayanan.Items.Add(o[2]);
                if ((string)lc == (string)o[2])
                {
                    cmbLayanan.SelectedIndex = i;
                    dgvPelayan.DataSource = Karyawan.DaftarKaryawan(Form1.idCabang, (string)o[1]);
                }
            }

            DataGridViewRow dr;
            for (int i = 0; i < dgvPelayan.Rows.Count; ++i)
            {
                dr = dgvPelayan.Rows[i];
                if ((string)k == dr.Cells[1].Value.ToString())
                    dgvPelayan.Rows[i].Cells[1].Selected = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection drc;
            DataGridViewRow dr;
            object[] o = new object[3]; //Menyimpan informasi mengenai layanan yg dipilih
            object[] c = new object[7]; //Untuk ditambahkan ke daftar layanan
            if (dgvPelayan.SelectedCells.Count > 0)
            {
                dr = dgvPelayan.CurrentRow;
                drc = dgvPelayan.SelectedRows;
                DataGridViewCellCollection dcc = dr.Cells;
                o = (object[])layananCabang[cmbLayanan.SelectedIndex];

                if (idTransaksiLayanan == 0)
                {
                    TransaksiLayanan t = new TransaksiLayanan();
                    t.AddTransaksiLayanan(idTransaksi, (int)dcc[0].Value, (int)o[0]);

                    c[0] = idTransaksi;
                    c[1] = t.id;
                    c[2] = Id;
                    c[3] = Name;
                    c[4] = dcc[1].Value.ToString();
                    c[5] = o[2];
                    c[6] = o[3];

                    layanan.Add(c);
                }
                else
                {
                    TransaksiLayanan t = new TransaksiLayanan();
                    t.UpdateTransaksiLayanan(idTransaksiLayanan, (int)dcc[0].Value, (int)o[0]);

                    c = (object[])layanan[findTransaksiLayanan(idTransaksiLayanan)];

                    c[4] = dcc[1].Value.ToString();
                    c[5] = o[2];
                    c[6] = o[3];

                    //layanan.Add(c);
                }

                Form2 f = new Form2(ref layanan, ref produk, idTransaksi, Id, Name, dgvTransaksi);
                f.Show();
                this.Close();
            }
        }

        private void dgvPelayan_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void cmbLayanan_SelectedIndexChanged(object sender, EventArgs e)
        {
            object[] o = (object[])layananCabang[cmbLayanan.SelectedIndex];

            dgvPelayan.DataSource = Karyawan.DaftarKaryawan(Form1.idCabang, (string)o[1]);
        }

        private int findTransaksiLayanan(int _idTransaksiLayanan)
        {
            object[] o;

            for (int i = 0; i < layanan.Count; ++i)
            {
                o = (object[])layanan[i];
                if ((int)o[1] == _idTransaksiLayanan)
                    return i;
            }

            return layanan.Count;
        }
    }
}
